package cn.edu.service;

import cn.edu.mapper.BrandMapper;
import cn.edu.pojo.Brand;
import cn.edu.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

/**
 * @author Davey chan
 */
public class BrandService {
    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
//    查询所有
     public List<Brand> selectAll(){
//     调用BrandMapper.selectAll()

     SqlSession sqlSession = sqlSessionFactory.openSession();
//      获取mapper对象
     BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
//     调用方法
     List<Brand> brands = mapper.selectAll();
     sqlSession.close();
     return brands;
 }
 public void add(Brand brand){
     //     调用BrandMapper.selectAll()

     SqlSession sqlSession = sqlSessionFactory.openSession();
//      获取mapper对象
     BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
     mapper.add(brand);
     sqlSession.commit();
     sqlSession.close();
 }
    public Brand selectById(int id){
//     调用BrandMapper.selectById()

        SqlSession sqlSession = sqlSessionFactory.openSession();
//      获取mapper对象
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
//     调用方法
     Brand brand = mapper.selectAllById(id);
        sqlSession.close();
        return brand;
    }
    public void update(Brand brand){
        //     调用BrandMapper.update()
        SqlSession sqlSession = sqlSessionFactory.openSession();
//      获取mapper对象
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
//        调用方法
        mapper.update(brand);
//        记得提交
        sqlSession.commit();
        sqlSession.close();
    }
}
